import React, { useRef, useState } from 'react'
import { getupload1 } from "../../request/index1";
interface Props {

}

const Index = (props: Props) => {
    let inp = useRef<HTMLInputElement>(null)
    const [url, setUrl] = useState('https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg')

    const upload = async () => {
        let file = (inp.current?.files as FileList)[0]
        console.log(file);
        if (!file) {
            alert('文件上传失败为空')
            setUrl('https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg')
            return
        }
        if (file.size / 1024 / 1024 > 0.5) {
            alert('文件过大')
            return
        }
        let formDate = new FormData()
        formDate.append('file', file)
        console.log(formDate);
        
        let res = await getupload1(formDate)
        console.log(res);
        if (res.data.code === 200) {
            alert('单文件上传成功')
            setUrl(res.data.result)
        }
    }

    return (
        <div>
            单文件上传
            <div>
                <input type='file' ref={inp} accept='image/*' multiple onChange={upload} />
                <img src={url} style={{ width: '300px', height: '200px' }} />
            </div>
        </div>
    )
}

export default Index
